var parentData, parentActive;
var dateFormatStr = 'yyyy-MM-dd HH:mm:ss';
layui.use(['form', 'table', 'layer', 'laydate'], function () {
	var form = layui.form,
		layer = layui.layer,
		table = layui.table,
		laydate = layui.laydate,
		$ = layui.$;

	window.active = {
		addPrize: () => {
			popEdit(parentData, 'page/gameLottery/addPrize.html', ["650px", "650px"]);
		},
		reload: () => {
			table.reload("tableListTable", {
				url: baseUrl('gameLottery/prize'),
				where: {
					id: parentData.id //搜索的关键字
				}
			});
		},
		editWinner: (data, obj) => {
			layer.prompt({
				value: data.winner_tel,
				title: '设置内定电话号码，多个号码用“;”间隔',
			}, async (value, index, elem) => {
				layer.close(index);

				data.winner_tel = js.trimStr(value);
				await ajaxAsync('gameLottery/prizeWinner', js.regroup(data, ['game_id', 'goods_id', 'winner_tel']), 'POST')
				obj.update(data);
			});
		},
		del: (data) => {
			popDelete(data.name, 'gameLottery/prize', js.regroup(data, ['game_id', 'goods_id', 'prize_level']));
		}
	};
	//操作
	$('.layui-btn').on('click', function () {
		var type = $(this).data('type');
		active[type] ? active[type].call(this) : '';
	});
	//表格头部按钮操作
	table.on('toolbar(tableList)', function (obj) {
		var type = obj.event;
		active[type] ? active[type].call(this, obj.data) : '';
	});
	//表格按钮操作
	table.on('tool(tableList)', function (obj) {
		var type = obj.event;
		active[type] ? active[type].call(this, obj.data, obj) : '';
	});

	//延时初始化，确保数据已经塞进来了
	setTimeout(() => {
		if (!parentData) return;
		//初始化数据
		form.val("dataform", JSON.parse(JSON.stringify(parentData)));
		//加载表格
		active.reload();
	}, 200);
	
	form.on("submit(save)",element=>{
		tableListTable=this.table.cache.tableListTable
		if(tableListTable.length==0){
			top.layer.alert('请增加奖品！');
		}else{
			var id = element.field.id
			unline('gameLottery/upline',{id})
		}
		return false
	})
});